/*
 * @Author: piaopiao.huang
 * @Date: 2021-12-30 09:30:31
 * @LastEditTime: 2022-01-19 10:23:05
 * @LastEditors: piaopiao.huang
 * @Description: In User Settings Edit
 */
import Vue from 'vue'

import App from './App'
import store from './store'
import router from './router'
import '@/utils/vant-ui.js'
import '@/assets/css/index.scss'
import '@/utils/element-ui.js'
import 'element-ui/lib/theme-chalk/index.css'
import permission from '@/directive/permission/index.js'
import Vconsole from 'vconsole'
new Vconsole()
import help from '@/utils/help'
import {DatetimePicker} from "vant"
import { Toast } from 'vant';
import {Checkbox} from "element-ui"
Vue.component(Checkbox.name,Checkbox)
Vue.use(DatetimePicker)
Vue.use(Toast)
Vue.directive('number', {
  bind(el) {
    el.oninput = () => {
      el.children[0].value = el.children[0].value.replace(/[^\d.]/g, '')
    }
  }
})
Vue.directive('draggable', {
  bind(el, binding, vnode) {
    let startX, startY
    let initialX, initialY
    let currentX = 0; let currentY = 0
    let xOffset = 0; let yOffset = 0
    let mousedown = false
    
    el.addEventListener('touchstart', onTouchStart)
    el.addEventListener('touchmove', onTouchMove)
    el.addEventListener('touchend', onTouchEnd)

    el.addEventListener('mousedown',onMouseStart)
    el.addEventListener('mousemove',onMouseMove)
    el.addEventListener('mouseup',onMouseEnd)
    el.addEventListener('mouseout',onMouseOut)
    function onTouchStart(event) { 
      initialX = currentX
      initialY = currentY

      startX =  event.touches[0].clientX - xOffset
      startY =  event.touches[0].clientY - yOffset

      if (binding.value && typeof binding.value.onDragStart === 'function') {
        binding.value.onDragStart()
      }
    }

    function onTouchMove(event) {
      currentX = event.touches[0].clientX - startX
      currentY =  event.touches[0].clientY - startY

      xOffset = currentX
      yOffset = currentY

      setTranslate(currentX, currentY)

      if (binding.value && typeof binding.value.onDrag === 'function') {
        binding.value.onDrag(currentX, currentY)
      }
    }

    function onTouchEnd() {
      initialX = currentX
      initialY = currentY

      if (binding.value && typeof binding.value.onDragEnd === 'function') {
        binding.value.onDragEnd()
      }
    }
    function onMouseStart(event) { 
      event.preventDefault();
      el.addEventListener('click', stopClick, true);
      mousedown= true
      initialX = currentX
      initialY = currentY

      startX = event.clientX - xOffset
      startY = event.clientY - yOffset
    }
    function onMouseMove(event){
      if(mousedown){
        event.preventDefault();
        // el.addEventListener('click', stopClick, true);
        currentX = event.clientX - startX
        currentY = event.clientY - startY
        
        xOffset = currentX
        yOffset = currentY

        setTranslate(currentX, currentY)
      }
    }
    function stopClick(event){
      event.stopPropagation();
      event.preventDefault();
    }
    function onMouseEnd(event){
      mousedown = false
      
      if(initialX == currentX && initialY == currentY){
        el.removeEventListener('click', stopClick, true);
      }else{
        el.addEventListener('click', stopClick, true);
      }
      initialX = currentX
      initialY = currentY
      if (binding.value && typeof binding.value.onDragEnd === 'function') {
        binding.value.onDragEnd()
      }
    }
    function onMouseOut(event){
      mousedown = false
    }
    function setTranslate(xPos, yPos) {
      el.style.transform = `translate3d(${xPos}px, ${yPos}px, 0)`
    }
  },

  unbind(el) {
    try {
      el.removeEventListener('touchstart', onTouchStart)
      el.removeEventListener('touchmove', onTouchMove)
      el.removeEventListener('touchend', onTouchEnd)
      el.removeEventListener('mousedown',onMouseStart)
      el.removeEventListener('mousemove',onMouseMove)
      el.removeEventListener('mouseup',onMouseEnd)
      el.removeEventListener('mouseout',onMouseOut)
    } catch (error) {
        console.log(error)
    }

  }
})
Vue.directive('integer-only', {
  bind(el) {
    el.addEventListener('input', event => {
      const value = event.target.value;
      event.target.value = value.replace(/\D/g, ''); // 只保留数字
    });
  }
})
window.time = help.time
// 阻止显示生产模式的消息
Vue.config.productionTip = false
Vue.use(permission)
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
}) 